package com.pikaqiu.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pikaqiu.blog.dao.ArticleCommentMapper;
import com.pikaqiu.blog.dao.ArticleMapper;
import com.pikaqiu.blog.dao.ArticleTypeMapper;
import com.pikaqiu.blog.dao.UserMapper;
import com.pikaqiu.blog.entity.Article;
import com.pikaqiu.blog.entity.ArticleComment;
import com.pikaqiu.blog.entity.ArticleType;
import com.pikaqiu.blog.service.ArticleService;
import com.pikaqiu.blog.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 吕梁山
 * @date 2019/1/7
 */
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private ArticleCommentMapper articleCommentMapper;

    @Resource
    private ArticleTypeMapper articleTypeMapper;

    @Override
    public PageInfo<Article> getArticlePage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleMapper.selectArticleList();
        for (Article article : articles) {
            article.setArticleContent(StringHelper.removeHtml(article.getArticleContent()));
        }
        return new PageInfo<>(articles);
    }

    @Override
    public Article getArticle(int id) {
        articleMapper.updateArticleViews(id);
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<ArticleComment> getArticleCommentPage(int articleId, int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<ArticleComment> list = articleCommentMapper.selectByArticleId(articleId);
        return new PageInfo<>(list);
    }

    @Override
    public List<ArticleType> getArticleTypeList() {
        return articleTypeMapper.getArticleTypeList();
    }

    @Override
    public void addArticleComment(ArticleComment articleComment) {
        articleCommentMapper.insertSelective(articleComment);
    }
}
